package com.ccn.ui.a2screenadaptation;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class A2_1_1ScreenAdapterLayout extends RelativeLayout {

    private boolean flag;

    public A2_1_1ScreenAdapterLayout(Context context) {
        this(context, null);
    }

    public A2_1_1ScreenAdapterLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public A2_1_1ScreenAdapterLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!flag) {
            float scaleX = ScreenAdaptationUtils.getInstance(getContext()).getHorizontalScale();
            float scaleY = ScreenAdaptationUtils.getInstance(getContext()).getVerticalScale();
            for (int i = 0; i < getChildCount(); i++) {
                View childAt = getChildAt(i);
                RelativeLayout.LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();
                layoutParams.width = (int) (layoutParams.width * scaleX);
                layoutParams.height = (int) (layoutParams.height * scaleY);
                layoutParams.leftMargin = (int) (layoutParams.leftMargin * scaleX);
                layoutParams.rightMargin = (int) (layoutParams.rightMargin * scaleY);

                layoutParams.topMargin = (int) (layoutParams.topMargin * scaleX);
                layoutParams.bottomMargin = (int) (layoutParams.bottomMargin * scaleY);
                childAt.setLayoutParams(layoutParams);
            }
            flag = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
